#!/bin/sh

# get params
. /etc/scripts/global.sh

# include kernel config
. /etc/scripts/config.sh

LOG="echo MODULES"

start() {
    get_param
    # if goahead started
    if [  -f /var/run/goahead.pid ]; then
	# clear conntrack and routes tables/caches
	flush_net_caches

	# wifi reconfigure only after goahead start
	$LOG "Prepare wifi config..."
	gen_wifi_config
	if [ "$OperationMode" = "2" ] && [ "$macCloneEnabled" = "1" ] && [ "$macCloneMac" != "" ]; then
	    WLAN_MAC_ADDR="$macCloneMac"
	    $LOG "STA Macclone mode. MAC: $WLAN_MAC_ADDR"
	else
	    $LOG "AP/STA Normal mode. MAC: $WLAN_MAC_ADDR"
	fi
	mac="mac=$WLAN_MAC_ADDR"

    	if [ "$OperationMode" = "2" ]; then
	    $LOG "Load wifi STA module..."
    	    modprobe -q rt2860v2_sta "$mac"
	else
	    $LOG "Load wifi AP module..."
    	    modprobe -q rt2860v2_ap  "$mac"
	fi
	if [ "$CONFIG_RT3090_AP" != "" ]; then
	    $LOG "Load RT3090 external wifi AP module..."
	    modprobe -q rt3090_ap "$mac"
	fi

	# mac must be set driver use module param
	if [ "$OperationMode" = "3" ]; then
	    $LOG "WiFi interface up APCLI..."
	    # apcli mode ap interface
	    ifconfig ra0 txqueuelen	"$txqueuelen"
	    ip link set ra0 up
	    # apcli mode sta interface
	    ifconfig apcli0 txqueuelen	"$txqueuelen"
	    ip link set apcli0 up
	else
	    $LOG "WiFi interface up..."
	    # others modes
	    ifconfig ra0 txqueuelen	"$txqueuelen"
	    ip link set ra0 up
	fi
	if [ "$CONFIG_RT3090_AP" != "" ]; then
	    $LOG "WiFi RT3090 interface up..."
	    # others modes
	    ifconfig rai0 txqueuelen	"$txqueuelen"
	    ip link set rai0 up
	fi
	$LOG "Tune wifi modules."
	retune_wifi
    else
	# if goahead not started
	# generate modules dep
	if [ ! -f /etc/modules.dep ]; then
	    $LOG "Start modules.dep generate..."
	    depmod -a -n > /etc/modules.dep
	fi
	# not load this modules after goahead start
	# load conntrack helpers modules
	load_nat_helpers
    fi
}

get_param() {
    eval `nvram_buf_get 2860 macCloneEnabled macCloneMac ipt_account`
}

gen_wifi_config() {
    rm -f /etc/Wireless/RT2860/RT2860.dat
    ralink_init make_wireless_config rt2860
}

load_nat_helpers() {
    # not load nf_nat helpers in bridge mode
    if [ "$OperationMode" != "0" ] && [ "$ApCliBridgeOnly" != "1" ]; then
	# load conntrack helpers
	eval `nvram_buf_get 2860 fwAlgTFTP fwAlgSIP fwAlgGRE fwAlgPPTP fwAlgH323 fwAlgFTP`
	if [ "$fwAlgTFTP" != "0" ]; then
	    modprobe -q nf_conntrack_tftp
	fi
	if [ "$fwAlgSIP" != "0" ]; then
	    modprobe -q nf_conntrack_sip
	    modprobe -q nf_nat_sip
	fi
	if [ "$fwAlgGRE" != "0" ]; then
	    modprobe -q nf_conntrack_proto_gre
	    modprobe -q nf_nat_proto_gre
	fi
	if [ "$fwAlgPPTP" != "0" ]; then
	    modprobe -q nf_conntrack_pptp
	    modprobe -q nf_nat_pptp
	fi
	if [ "$fwAlgH323" != "0" ]; then
	    modprobe -q nf_conntrack_h323
	    modprobe -q nf_nat_h323
	fi
	if [ "$fwAlgFTP" != "0" ]; then
	    modprobe -q nf_nat_ftp
	fi
    	if [ "$ipt_account" = "1" ]; then
	    modprobe -q ipt_account
	fi
    fi
}

wlan_down_all() {
    $LOG "Shutdown all wireless interfaces."
    # Flush ip address from all wireless interfaces, remove from bridge and down
    # First clear conntrack and routes tables/caches
    flush_net_caches
    # process iface shutdown
    for i in `seq 0 7`; do
	ip addr flush dev ra$i > /dev/null 2>&1
	if [ -d /proc/sys/net/ipv6 ] && [ "$IPv6_Enable" = "1" ]; then
    	    ip -6 addr flush dev ra$i > /dev/null 2>&1
	fi
	ip link set ra$i down > /dev/null 2>&1
	brctl delif br0 ra$i > /dev/null 2>&1
    done
    if [ "$CONFIG_RT2860V2_AP_WDS" != "" ]; then
	for i in `seq 0 3`; do
	    ip addr flush dev wds$i > /dev/null 2>&1
	    if [ -d /proc/sys/net/ipv6 ] && [ "$IPv6_Enable" = "1" ]; then
    		ip -6 addr flush dev wds$i > /dev/null 2>&1
	    fi
    	    ip link set wds$i down > /dev/null 2>&1
	    brctl delif br0 wds$i > /dev/null 2>&1
	done
    fi
    if [ "$CONFIG_RT2860V2_AP_APCLI" != "" ]; then
	ip addr flush dev apcli0 > /dev/null 2>&1
	if [ -d /proc/sys/net/ipv6 ] && [ "$IPv6_Enable" = "1" ]; then
    	    ip -6 addr flush dev apcli0 > /dev/null 2>&1
	fi
	ip link set apcli0 down > /dev/null 2>&1
	brctl delif br0 apcli0 > /dev/null 2>&1
    fi
    if [ "$CONFIG_RT2860V2_STA_MESH" != "" ] || [ "$CONFIG_RT2860V2_AP_MESH" != "" ]; then
	ip addr flush dev mesh0 > /dev/null 2>&1
	if [ -d /proc/sys/net/ipv6 ] && [ "$IPv6_Enable" = "1" ]; then
    	    ip -6 addr flush dev mesh0 > /dev/null 2>&1
	fi
	ip link set mesh0 down > /dev/null 2>&1
	brctl delif br0 mesh0 > /dev/null 2>&1
    fi
    if [ "$CONFIG_RT3090_AP" != "" ]; then
	for i in `seq 0 7`; do
	    ip addr flush dev rai$i > /dev/null 2>&1
	    if [ -d /proc/sys/net/ipv6 ] && [ "$IPv6_Enable" = "1" ]; then
    		ip -6 addr flush dev rai$i > /dev/null 2>&1
	    fi
	    ip link set rai$i down > /dev/null 2>&1
	    brctl delif br0 rai$i > /dev/null 2>&1
	done
    fi
}

retune_wifi() {
    # preconfigure wifi
    /etc/scripts/wifi.sh ra0
    if [ "$CONFIG_RT3090_AP" != "" ]; then
	/etc/scripts/wifi.sh rai0
    fi
}

stop() {
    get_param
    if [ -f /var/run/goahead.pid ]; then
	$LOG "link wifi down first"
	wlan_down_all
	if [ -d /sys/module/hw_nat ]; then
	    $LOG "Unload hw_nat and wireless modules..."
	    mod="hw_nat rt2860v2_ap rt2860v2_sta rt3090_ap"
	else
	    $LOG "Unload wireless modules..."
	    mod="rt2860v2_ap rt2860v2_sta rt3090_ap"
	fi
	for module in $mod
	do
	    if [ -d /sys/module/$module ]; then
		rmmod $module > /dev/null 2>&1
	    fi
	done
    fi
}

case "$1" in

	start)
	    start
	    ;;

	stop)
	    stop
	    ;;

	wlan_down_all)
	    wlan_down_all
	    ;;

	retune_wifi)
	    retune_wifi
	    ;;

	restart)
	    stop
	    start
	    ;;

	*)
	    echo $"Usage: $0 {start|stop|restart|wlan_down_all|retune_wifi}"
	    exit 1
esac
